﻿using System;
using System.Drawing;
using System.Windows.Forms;
using monitor_2_DBwork;
using System.Linq;

namespace monitor_server_2
{
    public partial class AddDevice : Form
    {
        private readonly TreeNode _node;
        private DeviceList.RefreshTree d;
        public AddDevice(TreeNode node, DeviceList.RefreshTree del)
        {
            InitializeComponent();
            d = del;
            rtpsBox.DropDownStyle = ComboBoxStyle.DropDownList;
            nameBox.DropDownStyle = ComboBoxStyle.DropDownList;
            _node = node;
            StartPosition = FormStartPosition.CenterScreen;
            acceptButton.Text = Main.Rm.GetString("Accept");
            cancelButton.Text = Main.Rm.GetString("Cancel");
        }

        private void AddDevice_Load(object sender, EventArgs e)
        {
            ipPictureBox.Image = (Image) Main.Rm.GetObject("network_22");
            acceptButton.Image = (Image) Main.Rm.GetObject("accept_image_16");
            cancelButton.Image = (Image)Main.Rm.GetObject("cancel_image_16");

            Text = Main.Rm.GetString("AddDevice");
            var rtpsList = DBwork.GetRTPSList();
            if (rtpsList.Count <= 1)
            {
                if (rtpsList.Count == 0)
                    return;
                if (rtpsList[0].Contains("err:"))
                {
                    MessageBox.Show(rtpsList[0].Substring(4));
                    return;
                }
            }

            rtpsBox.Items.AddRange(rtpsList.ToArray());
            rtpsBox.SelectedItem = _node.Text;

            var deviceList = DBwork.GetDeviceList();
            nameBox.Items.AddRange(deviceList.ToArray());

            rtpsLabel.Text = Main.Rm.GetString("Station");
            nameLabel.Text = Main.Rm.GetString("Name");
            ipLabel.Text = Main.Rm.GetString("IPAddress");
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void acceptButton_Click(object sender, EventArgs e)
        {
            if (rtpsBox.SelectedItem == null)
            {
                MessageBox.Show(Main.Rm.GetString("WrongParameter") + @": " + Main.Rm.GetString("Station"));
                return;
            }
            if (nameBox.SelectedItem == null)
            {
                MessageBox.Show(Main.Rm.GetString("WrongParameter") + @": " + Main.Rm.GetString("Name"));
                return;
            }
            System.Net.IPAddress ip;
            if (!System.Net.IPAddress.TryParse(ipBox.Text, out ip))
            {
                MessageBox.Show(Main.Rm.GetString("WrongParameter") + @": " + Main.Rm.GetString("IPAddress"));
                return;
            }
            if (DBwork.CheckDeviceIp(ip.ToString()))
            {
                MessageBox.Show(Main.Rm.GetString("IPAddress") + @" " + Main.Rm.GetString("Exist").ToLower());
                return;
            }

            if (!DBwork.AddDevice(nameBox.Text, rtpsBox.Text, ip.ToString()))
            {
                MessageBox.Show(Main.Rm.GetString("Error"));
            }
            else
            {
                MessageBox.Show(Main.Rm.GetString("Complete"));
                d();
                Close();
            }
        }

        private void AddDevice_Shown(object sender, EventArgs e)
        {
            Icon = (Icon) Main.Rm.GetObject("device_16_ico");
        }
    }
}
